package net.randomice.emf.observables.generator.util;

import org.eclipse.emf.codegen.ecore.genmodel.GenClass;
import org.eclipse.emf.codegen.ecore.genmodel.GenClassifier;
import org.eclipse.emf.codegen.ecore.genmodel.GenDataType;
import org.eclipse.emf.codegen.ecore.genmodel.GenFeature;

public class GenFeatureHelper {

	public static String getQualifiedFeatureAccessor(GenFeature f) {
		return f.getQualifiedFeatureAccessor();
	}

	public static String getQualifiedIdAccessor(GenFeature f) {
		return f.getGenPackage().getImportedPackageInterfaceName() + "."
				+ f.getGenClass().getFeatureID(f);

	}

	public static String getSetAccessor(GenFeature f) {
		String getAccessor = f.getGetAccessor();
		return "set"
				+ (getAccessor.startsWith("get") ? getAccessor.substring(3)
						: getAccessor.startsWith("is") ? getAccessor
								.substring(2) : getAccessor);
	}

	public static GenClass getTypeGenClass(GenFeature f) {
		return f.getTypeGenClass();
	}

	public static GenClassifier getTypeGenClassifier(GenFeature f) {
		return f.getTypeGenClassifier();
	}

	public static GenDataType getTypeGenDataType(GenFeature f) {
		return f.getTypeGenDataType();
	}

	public static String getImportedType(GenFeature f) {
		return f.getImportedType();
	}

	public static String getImportedCastableType(GenFeature f) {
		if (f.isPrimitiveType()) {
			Class<?> c = f.getEcoreFeature().getEType().getInstanceClass();
			if (Integer.TYPE.equals(c)) {
				return "Integer";
			}
			String name = c.getSimpleName();
			return name.substring(0, 1).toUpperCase() + name.substring(1);
		} else {
			return getImportedType(f);
		}
	}
}
